package com.czkj.hwz.decorate.control;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.czkj.hwz.decorate.IMP.HttpDoImp;
import com.czkj.hwz.decorate.base.HttpUrl;
import com.czkj.hwz.decorate.bean.Goods;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/6.
 */

public class GoodsGet {
    private Context mContext;
    private HttpDoImp mHttpDoImp;
    public GoodsGet(Context mContext) {
        this.mContext = mContext;
        this.mHttpDoImp = (HttpDoImp)mContext;
    }
    public GoodsGet(Context mContext,HttpDoImp httpDoImp){
        this.mContext = mContext;
        this.mHttpDoImp = httpDoImp;
    }
    public void getList(HashMap<String,String> queryItems){
        RequestParams params = new RequestParams(HttpUrl.GOODS_GET);
        if (queryItems != null) {
            Iterator it = queryItems.entrySet().iterator();
            while(it.hasNext()){
                Map.Entry m=(Map.Entry)it.next();
               params.addQueryStringParameter(String.valueOf(m.getKey()),String.valueOf(m.getValue()));
            }

            x.http().get(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                  //  Toast.makeText(mContext,result,Toast.LENGTH_LONG).show();
                    JSONObject resultJson ;
                    int total;
                    List<Goods> rows;
                    try {
                        resultJson = new JSONObject(result);
                        total = resultJson.getInt("total");
                        rows = JSON.parseArray(resultJson.getString("rows"),Goods.class);
                    }catch (Exception e) {
                        total = -1;
                        rows = null;

                    }

                    mHttpDoImp.onSuccess("getList",total,rows);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    mHttpDoImp.onError(ex.getMessage());
                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }
}
